<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>问答信息管理</span>
        <el-button class="button" type="primary" @click="btnAddDialog">
          添加</el-button
        >
      </div>
    </template>
    <!-- 查询 -->
    <el-form :inline="true" :model="questionform" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input
          v-model="searchInput"
          placeholder="请输入搜索内容"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getQuestions">查询</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData2" border style="width: 100%">
      <el-table-column prop="q_uid" label="uid" width="200" />
      <el-table-column prop="q_name" label="姓名" width="200" />
      <el-table-column prop="q_context" label="标题" width="200"/>
      <el-table-column prop="q_context1" label="详情" width="400">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="">
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="detailDialog(scope.row.q_uid)"
            >查看</el-button
          >
          <el-button
            link
            type="primary"
            size="small"
            @click="updateDialog(scope.row.q_uid)"
            >编辑</el-button
          >

          <el-popconfirm
            confirm-button-text="是"
            cancel-button-text="否"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="Are you sure to delete this?"
            @confirm="deleteClick(scope.row.q_uid)"
          >
            <template #reference>
              <el-button link type="warning" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <br />
          <!-- 分页 -->
     <el-pagination
      :currentPage="currentPage"
      :page-size="pageSize"
      :page-sizes="[5,10, 20, 30, 40]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    /> 
  </el-card>
  <!-- 回答对话框 -->
  <el-dialog v-model="detailDialogVisible" title="查看问题的回答" width="60%">
    <el-table :data="tableData1" style="width: 100%">
      <el-table-column prop="a_uid" label="uid" width="200" />
      <el-table-column prop="a_name" label="姓名" width="200" />
      <el-table-column prop="a_context" label="回答" width="500">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="160">
        <template #default="scope">

          <el-popconfirm
            confirm-button-text="是"
            cancel-button-text="否"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="Are you sure to delete this?"
            @confirm="deleteClick2(scope.row.a_uid)"
          >
            <template #reference>
              <el-button link type="warning" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
  <!-- 添加对话框 -->
  <el-dialog v-model="dialogFormVisible" title="添加用户信息">
    <el-form :model="form">
      <el-form-item label="用户名" :label-width="formLabelWidth">
        <el-input v-model="form.q_name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="提问标题" :label-width="formLabelWidth">
        <el-input v-model="form.q_context" autocomplete="off" />
      </el-form-item>
      <el-form-item label="提问详情" :label-width="formLabelWidth">
        <el-input v-model="form.q_context1" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addUser"> 添加 </el-button>
      </span>
    </template>
  </el-dialog>
  <!-- 修改对话框 -->
  <el-dialog v-model="updateDialogVisible" title="修改提问内容">
    <el-form :model="form">
      <el-form :model="form">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input v-model="form.q_name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="提问标题" :label-width="formLabelWidth">
          <el-input v-model="form.q_context" autocomplete="off" />
        </el-form-item>
        <el-form-item label="提问内容" :label-width="formLabelWidth">
          <el-input v-model="form.q_context1" autocomplete="off" />
        </el-form-item>
      </el-form>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateQuestion">修改</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      pageInfo: {},
      formLabelWidth: "140px",
      form: {},
      dialogFormVisible: false, //添加对话框
      detailDialogVisible: false, //查看回答对话框
      updateDialogVisible: false, //修改对话框
      tableData: [],
      ////提问有关变量
      questionform: {
        q_uid: "",
        q_name:""
      },
      askform:{
        a_uid: "",
        a_name:"",
        a_context:"",
        q_uid:""
      },
      askform1:{
        q_uid: "",
      },
      tableData1: [],
      tableData2: [],
      searchInput: '',
    };
  }, //
  mounted() {
    // 加载用户信息
    this.Questionform();
    this.Askform();
    this.getData(1,5) //分页
  },
  methods: {
      // 分页
      getData(num,size) {
        this.$http.get("http://localhost:8080/questionform/page", {
          params: {
            pageNum: num,
            pageSize: size,
          },
        }
        ).then((response) => {
          this.pageInfo = response.data
          this.tableData2 = this.pageInfo.list 
        }).catch((err) => {
          console.log(err);
      })
    },
      handleSizeChange(pageSize){
        this.pageSize = pageSize
        this.getData(this.currentPage,this.pageSize)
        console.log("size",pageSize)

      },
      handleCurrentChange(pageNum){
        this.currentPage = pageNum
        this.getData(this.currentPage,this.pageSize)
        console.log("num",pageNum)
      },
    Questionform() {      // 加载提问信息数据
      this.$http
        .get("http://localhost:8080/questionform/list", this.questionform)
        .then((res) => {
          this.tableData2 = res.data;
          console.log(res.data);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    updateQuestion() {//提问修改操作
      console.log(this.form);
      this.$http
        .post("http://localhost:8080/questionform/update", this.form)
        .then((res) => {
          console.log(res.data);
          if (res.data == 1) {
            console.log("修改成功！");
            this.form = {}; // 清空表单对象
            this.updateDialogVisible = false; // 关闭修改对话框
            this.Questionform(); // 重新加载数据
            ElMessage({
              message: "修改成功！",
              type: "success",
            });
          } else {
            console.log("修改失败！");
            ElMessage({
              message: "修改失败！",
              type: "warning",
            });
          }
        })
        .catch((err) => {});
    },
    updateDialog(q_uid) {      // 提问编辑操作
      console.log(q_uid);
      this.$http
        .get("http://localhost:8080/questionform/get/" + q_uid)
        .then((res) => {
          console.log(res.data);
          this.form = res.data;
        })
        .catch((err) => {});
      this.updateDialogVisible = true;
    },
    updateDialog2(q_uid) {      // 回答编辑操作
      console.log(q_uid);
      this.$http
        .get("http://localhost:8080/askform/get",)
        .then((res) => {
          console.log(res.data);
          this.form = res.data;
        })
        .catch((err) => {});
      this.updateDialogVisible = true;
    },
    addUser() {//添加
      console.log(this.form);
      this.$http
        .post("http://localhost:8080/questionform/add", this.form)
        .then((res) => {
          console.log(res.data);
          if (res.data == 1) {
            console.log("添加成功！");
            this.form = {};
            this.dialogFormVisible = false;
            this.Questionform();
            ElMessage({
              message: "添加成功！",
              type: "success",
            });
          } else {
            console.log("添加失败！");
            ElMessage({
              message: "添加失败！",
              type: "warning",
            });
          }
        })
        .catch((err) => {});
    },

    deleteClick(q_uid) {      // 删除操作
      console.log(q_uid);
      this.$http
        .get("http://localhost:8080/questionform/del/" + q_uid)
        .then((res) => {
          console.log(res.data);
          if (res.data == 1) {
            console.log("删除成功！");
            this.Questionform();
            ElMessage({
              message: "删除成功！",
              type: "success",
            });
          } else {
            console.log("删除失败！");
            ElMessage({
              message: "删除失败！",
              type: "warning",
            });
          }
        })
        .catch((err) => {});
    },
    deleteClick2(a_uid) {      // 删除回答操作
      console.log(a_uid);
      this.$http
        .get("http://localhost:8080/askform/del/" + a_uid)
        .then((res) => {
          console.log(res.data);
          if (res.data == 1) {
            console.log("删除成功！");
            this.Questionform();
            ElMessage({
              message: "删除成功！",
              type: "success",
            });
          } else {
            console.log("删除失败！");
            ElMessage({
              message: "删除失败！",
              type: "warning",
            });
          }
        })
        .catch((err) => {});
    },
    Askform() { // 加载提问信息数据 
      this.$http
        .get("http://localhost:8080/askform/list", this.askform)
        .then((res) => {
          this.tableData1 = res.data;
          console.log(res.data);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    detailDialog(q_uid) { // 查看回答操作 
      console.log(q_uid);

      // 在这里获取回答信息
      this.$http.get(`http://localhost:8080/askform/getByQUid/${q_uid}`)
        .then((res) => {
          console.log(res.data);
          this.tableData1 = res.data;
        })
        .catch((err) => {
          console.log(err);
        });

      this.detailDialogVisible = true;
    },

    getQuestions() {
    const search = this.searchInput; // 获取用户输入
    console.log("Search input:", search);  // 打印输入值
    if (!search) {
        console.log("请输入搜索内容");
        return;
    }

    this.$http.get(`http://localhost:8080/questionform/query?search=${encodeURIComponent(search)}`)
        .then((res) => {
            this.tableData2 = res.data; // 假设这是您存放结果的变量
            console.log(res.data);
            console.log("Received data:", res.data);  // 打印接收到的数据
        })
        .catch((err) => {
            console.log(err);
        });
},







    btnAddDialog() {
      this.dialogFormVisible = true;
    },
  },
};
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 100%;
}
</style>
